using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using vs.Core;

namespace Forum
{
    public partial class forum_new : System.Web.UI.UserControl
    {
        private bool validating()
        {
            txtForumID.Text = txtForumID.Text.Trim();

            if (txtForumID.Text == "")
            {
                lblMessage.Text = "You cannot left blank ForumID";
                lblMessage.Visible = true;
                return false;
            }

            if (!CommonFunction.validate_key(txtForumID.Text))
            {
                lblMessage.Text = "ForumID is not valided, it accept : A..Z, a..z, -, _ ";
                lblMessage.Visible = true;
                return false;
            }

            try
            {
                string strSQL = "SELECT Count(*) FROM FORUM_Forum WHERE ForumID = @ForumID";
                DataParameter p = new DataParameter("@ForumID", txtForumID.Text);
                object o = DataFactory.ExecuteScalar(strSQL, p);
                int count = 0;
                if (o != null) count = Convert.ToInt32(o);

                if (count >= 1)
                {
                    lblMessage.Text = "ForumID is exist";
                    lblMessage.Visible = true;
                    return false;
                }
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
                lblMessage.Visible = true;
                return false;
            }

            try
            {
                int i = Convert.ToInt32(txtOrderIndex.Text);
            }
            catch
            {
                lblMessage.Text = "You have to enter a number";
                lblMessage.Visible = true;
                return false;
            }

            return true;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                lblCatID.Text = Request.QueryString["ID"];
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (!validating()) return;

                ForumInfo fr = new ForumInfo();
                fr.CategoryID = lblCatID.Text;
                fr.Description = txtDescription.Text;
                fr.ForumID = txtForumID.Text;
                fr.ForumName = txtName.Text;
                fr.OrderIndex = Convert.ToInt32(txtOrderIndex.Text);

                ForumController db = new ForumController();
                db.Insert(fr);

                btnCancel_Click(null, new EventArgs());
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
                lblMessage.Visible = true;
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(UrlProvider.AdminUrl("fr_forum", "admin"));
        }
    }
}